import Request from 'utils/request';

import CouponTaoData from 'models/sysCoupon/couponTaoData';
import CouponTaoDetail from 'models/sysCoupon/couponTaoDetail';
import CouponData from 'models/sysCoupon/couponData';
import MyCouponData from 'models/sysCoupon/myCouponData';

export default class CouponApi {
  static getTaoCouponClassifyList() {
    const apiName = 'coupon/taobao/goodsClass';

    return Request.post(apiName).then(res => {
      let classifyList = res.data;
      classifyList = classifyList.map(obj => obj.name);
      return classifyList;
    });
  }

  static getTaoCouponData(pagination, classify) {
    const apiName = 'coupon/taobao/getCouponListByClass';

    return Request.post(apiName, {
      keyword: classify,
      minId: pagination.currentPage
    }).then(res => {
      let coupons = res.data;
      let couponList = [];
      for (let i = 0; i < coupons.length; i++) {
        let coupon = coupons[i];
        coupon = new CouponTaoData(coupon);
        couponList.push(coupon);
      }

      pagination.currentPage = res.rows;

      return couponList;
    });
  }

  static searchTaoCouponData(pagination, searchValue) {
    const apiName = 'coupon/taobao/getCouponListByGoodsName';

    return Request.post(apiName, {
      keyword: searchValue,
      minId: pagination.currentPage
    }).then(res => {
      let coupons = res.data;
      let couponList = [];
      for (let i = 0; i < coupons.length; i++) {
        let coupon = coupons[i];
        coupon = new CouponTaoData(coupon);
        couponList.push(coupon);
      }

      pagination.currentPage = res.rows;

      return couponList;
    });
  }

  static getTaoCouponDetail(itemId) {
    const apiName = 'coupon/taobao/getCouponInfoByGoodsId';

    return Request.post(apiName, {
      itemId
    }).then(res => {
      let couponDetail = res.data;
      couponDetail = new CouponTaoDetail(couponDetail);
      return couponDetail;
    });
  }

  static getTaoPassword(couponDetail) {
    const apiName = 'coupon/taobao/getCouponTklByGoodsId';

    return Request.post(apiName, {
      itemId: couponDetail.itemId,
      activityId: couponDetail.activityId
    }).then(res => {
      let obj = res.data;
      if (obj === null) {
        return '';
      }

      return obj.long_coupon_tpwd;
    });
  }

  static getCouponClassifyList() {
    const apiName = 'coupon/mantaotao/getCouponClass';
    return Request.post(apiName).then(res => {
      let classifyList = res.data;
      classifyList = classifyList.map(obj => {
        return {
          id: obj.majorTypeId,
          name: obj.majorName
        };
      });
      return classifyList;
    });
  }

  static getCouponData(typeId, lnglat, pagination) {
    const apiName = 'coupon/mantaotao/getCouponListByClass';
    let postData = {
      majorTypeId: typeId,
      latitude: lnglat.lat,
      longitude: lnglat.lng,
      limit: pagination.pageSize,
      page: pagination.currentPage
    };
    return Request.post(apiName, postData).then(res => {
      let coupons = res.data;
      let couponList = [];
      for (let i = 0; i < coupons.length; i++) {
        let coupon = coupons[i];
        coupon = new CouponData(coupon);
        couponList.push(coupon);
      }

      pagination.currentPage = res.rows;

      return couponList;
    });
  }

  static collectCoupon(coupon) {
    const apiName = 'coupon/mantaotao/getCoupon';
    let id = coupon.id;
    return Request.post(apiName, {
      stockId: coupon.stockId
    }).then(() => {
      return this.getCouponById(id);
    });
  }

  static getCouponById(id) {
    const apiName = 'coupon/mantaotao/getCouponInfoById';
    return Request.post(apiName, {
      id
    }).then(res => {
      let coupon = res.data;
      coupon = new CouponData(coupon);
      return coupon;
    });
  }

  static searchCouponData(searchValue, lnglat, pagination) {
    const apiName = 'coupon/mantaotao/getCouponListBySearch';
    let postData = {
      keyWord: searchValue,
      latitude: lnglat.lat,
      longitude: lnglat.lng,
      limit: pagination.pageSize,
      page: pagination.currentPage
    };

    return Request.post(apiName, postData).then(res => {
      let coupons = res.data;
      let couponList = [];
      for (let i = 0; i < coupons.length; i++) {
        let coupon = coupons[i];
        coupon = new CouponData(coupon);
        couponList.push(coupon);
      }
      pagination.currentPage = res.rows;
      return couponList;
    });
  }

  static getMyCouponList(pagination) {
    const apiName = 'coupon/mantaotao/myCouponsList';

    return Request.post(apiName, {
      limit: pagination.pageSize,
      page: pagination.currentPage - 1
    }).then(res => {
      let coupons = res.data;
      let couponList = [];
      for (let i = 0; i < coupons.length; i++) {
        let coupon = coupons[i];
        coupon = new MyCouponData(coupon);
        couponList.push(coupon);
      }
      pagination.currentPage = res.rows;
      return couponList;
    });
  }
}
